package com.guaika.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guaika.reggie.common.CustomException;
import com.guaika.reggie.common.ResultMessage;
import com.guaika.reggie.common.StringChars;
import com.guaika.reggie.dto.SetmealDto;
import com.guaika.reggie.entity.Category;
import com.guaika.reggie.entity.Setmeal;
import com.guaika.reggie.entity.SetmealDish;
import com.guaika.reggie.mapper.SetmealMapper;
import com.guaika.reggie.service.CategoryService;
import com.guaika.reggie.service.SetmealDishService;
import com.guaika.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author GuaiKa
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public void saveWidthSetmeal(SetmealDto setmealDto) {
        //保存套餐信息
        this.save(setmealDto);
        //套餐菜品关系，补充套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //批量保存
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void deleteWidthDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据id判断该套餐是否在售卖中
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        if(this.count(setmealLambdaQueryWrapper)>0){
            throw new CustomException(StringChars.DELETE_SETMEAL_STATUS_ERROR.getStr());
        }
        //删除套餐关系表中的数据
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);
    }

    @Override
    public ResultMessage<Page> setmealPage(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(name !=null,Setmeal::getName, name);
        this.page(setmealPage,setmealLambdaQueryWrapper);

        //当前实体类无法满足前端展示需求，使用dto类进行填充返回
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto>  setmealDtoList = records.stream().map(item->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            if(category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        return ResultMessage.success(setmealDtoPage.setRecords(setmealDtoList));
    }

    @Override
    public ResultMessage<List<Setmeal>> setmealList(Setmeal setmeal) {
        //获取wrapper对象
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //构建条件
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, setmeal.getStatus());
        //返回查询结果
        return ResultMessage.success(this.list(setmealLambdaQueryWrapper));
    }
}
